#include "WebPage.hpp"
#include "../include/simhash/Simhasher.hpp"

using namespace simhash;

/*路径信息*/
#define JIEBAPATH "../include/cppjieba/dict/jieba.dict.utf8"
#define HMMPATH   "../include/cppjieba/dict/hmm_model.utf8"
#define IDFPATH   "../include/cppjieba/dict/idf.utf8"   
#define STOPPATH  "../include/cppjieba/dict/stop_words.utf8"

/* Simhasher _sim("../include/cppjieba/dict/jieba.dict.utf8", */                                 
/*       "../include/cppjieba/dict/hmm_model.utf8", */
/*       "../include/cppjieba/dict/idf.utf8", */
/*       "../include/cppjieba/dict/stop_words.utf8"); */

Simhasher sim(JIEBAPATH,HMMPATH,IDFPATH,STOPPATH);

WebPage::WebPage(Configuration* config, WordSegmentation &jieba, 
        int &docId, const string &docTitle,
        const string &docUrl, const string&docContent)
: _docId(docId)
, _docTitle(docTitle)
, _docUrl(docUrl)
, _docContent(docContent)
{
    _doc = _docTitle + _docContent;
    processDoc(_doc, config, jieba);
    getSimhashvalue();
}

int WebPage::getDocId(){
    return _docId;
}

string WebPage::getDoc(){
    return _doc;
}

map<string, int> & WebPage::getWordsMap(){
    return _wordsMap;
}
string WebPage::getTitle(){
    return _docTitle;
}

string WebPage::getUrl(){
    return _docUrl;
}

string WebPage::getContent(){
    return _docContent;
}

void WebPage::reSetDocId(int docId)
{
    _docId = docId;
}


void WebPage::processDoc(const string& doc, Configuration* config, WordSegmentation& jieba){

    vector<string> tmp = jieba.cut(_doc);

    set<string>& stopWords = config->getStopWordList();

    for(auto & word: tmp){
        if(stopWords.count(word)){
            continue;
        }
        
        if(_wordsMap.count(word)){
            ++_wordsMap[word];
        }
        else{
            _wordsMap[word] = 1;
        }
    }
    //test 
    /* for(auto& p: _wordsMap){ */
    /*     cout << p.first << " : " << p.second << endl; */
    /* } */

}

void WebPage::getSimhashvalue()
{
    string s1 = _docContent;
    size_t topN = 5;
    _simhashValue = 0;
    vector<pair<string,double>> resS1;

    sim.extract(s1,resS1,topN);
    sim.make(s1,topN,_simhashValue);

}

bool operator== (const WebPage &lhs, const WebPage &rhs)
{
    bool isdelete = Simhasher::isEqual(lhs._simhashValue,rhs._simhashValue);
    if(isdelete)
    {
        return true;
    }
    return false;

}


bool operator< (const WebPage & lhs, const WebPage &rhs)
{
    if(lhs._docId < rhs._docId)
    {
        return true;
    }
    else
    {
        return false;
    }
}

